package com.fortune.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.UUID;

import com.fortune.exception.db.UpdateInnerErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fortune.dao.DistributorOrderMgtDao;
import com.fortune.dto.in.distributorOrder.DistributorOrderAddDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderDetailAddDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderEditDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderListDto;
import com.fortune.entity.MProductInfo;
import com.fortune.entity.MUserInfo;
import com.fortune.service.DistributorOrderMgtService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.distributorOrderMgt.DistributorOrder;
import com.fortune.vo.distributorOrderMgt.DistributorOrderDetail;
import com.fortune.vo.distributorOrderMgt.Product;
import com.fortune.vo.distributorOrderMgt.ProductDetail;

@Service
public class DistributorOrderMgtServiceImpl implements DistributorOrderMgtService {
	@Autowired
	private DistributorOrderMgtDao distributorOrderMgtDao;
	@Override
	public List<DistributorOrder> list(MUserInfo mUserInfo,DistributorOrderListDto distributorOrderMgtListDto) {
		String type = distributorOrderMgtListDto.getType().toString();
		List<DistributorOrder> list = distributorOrderMgtDao.list(mUserInfo,distributorOrderMgtListDto,type);
		return list;
	}
	
	@Override
	public int count(MUserInfo mUserInfo,DistributorOrderListDto distributorOrderMgtListDto) {
		String type = distributorOrderMgtListDto.getType().toString();
		int count = distributorOrderMgtDao.count(mUserInfo,distributorOrderMgtListDto,type);
		return count;
	}
	
	@Override
	public DistributorOrderDetail detail(String mId, String dId, String orderId) {
		DistributorOrderDetail detail = distributorOrderMgtDao.detail(mId, dId, orderId);
		return detail;
	}
	@Override
	@Transactional
	public int add(DistributorOrderAddDto addDto,String mId,String createUser) {
		String id = UUID.randomUUID().toString().replace("-","");
		List<DistributorOrderDetailAddDto> list = addDto.getDetails();
		BigDecimal pTotalPrice = new BigDecimal(0);
		BigDecimal dTotalPrice = new BigDecimal(0);
		int i = 0;
		for(DistributorOrderDetailAddDto distributorOrderDetailAddDto: list){
			i = i+1;
			int num = distributorOrderDetailAddDto.getOrderNum();
	        if(distributorOrderDetailAddDto.getMcdid() == null){
	        	distributorOrderDetailAddDto.setMcdid("0");
			}
			int add = distributorOrderMgtDao.addDetail(distributorOrderDetailAddDto, mId, addDto.getDistributorId(), id,i);
			if(add < 1){
				throw new UpdateInnerErrorException("新增失败");
			}
			pTotalPrice = pTotalPrice.add(distributorOrderDetailAddDto.getProTotal());
			dTotalPrice = dTotalPrice.add(distributorOrderDetailAddDto.getProDeposit());
		}
		int add = distributorOrderMgtDao.add(addDto, mId,createUser,id,pTotalPrice,dTotalPrice);
		return add;
	}
	@Override
	@Transactional
	public int edit(DistributorOrderEditDto editDto,String mId,String createUser) {
		String id = editDto.getOrderId();
		String dId = editDto.getDistributorId();
		int delete = distributorOrderMgtDao.deleteDetal(mId, dId, id);
		List<DistributorOrderDetailAddDto> list = editDto.getDetails();
		BigDecimal pTotalPrice = new BigDecimal(0);
		BigDecimal dTotalPrice = new BigDecimal(0);
		int i =0;
		for(DistributorOrderDetailAddDto distributorOrderDetailAddDto: list){
			i = i+1;
			if(distributorOrderDetailAddDto.getMcdid() == null){
				distributorOrderDetailAddDto.setMcdid("0");
			}
			int add = distributorOrderMgtDao.addDetail(distributorOrderDetailAddDto, mId, editDto.getDistributorId(), id,i);
			if(add < 1){
				throw new UpdateInnerErrorException("编辑失败");
			}
			pTotalPrice = pTotalPrice.add(distributorOrderDetailAddDto.getProTotal());
			dTotalPrice = dTotalPrice.add(distributorOrderDetailAddDto.getProDeposit());
		}
		System.out.println("editDto:"+editDto+"-------"+"mid:"+mId+"-------"+"createUser:"+createUser+"-------"+"pTotalPrice:"+pTotalPrice+"-------"+"dTotalPrice"+dTotalPrice);
		int result = distributorOrderMgtDao.edit(editDto, mId, createUser, pTotalPrice, dTotalPrice);
		return result;
	}
	@Override
	public int delete(String mId, String dId, String orderId) {
		int result = distributorOrderMgtDao.delete(mId, dId, orderId);
		return result;
	}
	@Override
	@Transactional
	public synchronized int approve(String name,String mId, String dId, String orderId,String shipStore,String createUser) {
		List<Product> list = distributorOrderMgtDao.productDetails(mId, dId, orderId);
		if (list.isEmpty()) {
			return 0;
		}
		for (Product product : list) {
			int sum = product.getOrderNum();
			String id = product.getMpid();
			int count = distributorOrderMgtDao.storeCount(mId,id,shipStore);
			int outCount = distributorOrderMgtDao.outCount(mId,id,shipStore);
			int transCount = distributorOrderMgtDao.transCount(mId, id, shipStore);
			if (sum>(count-outCount-transCount)) {
				return 2;
			}
		}
		String shipUser = createUser;/*distributorOrderMgtDao.shipUser(mId, shipStore);*/
		DistributorOrderDetail detail = distributorOrderMgtDao.detail(mId, dId, orderId);
		String uuid = UUIDUtil.get32UUID();
		if(detail.getPayTypeId() == null){
			detail.setPayTypeId("0");
		}
		Integer num = distributorOrderMgtDao.findOutNum(mId);
		DecimalFormat countFormat = new DecimalFormat("000000");
		String aa;
		if(num == null){
			aa = "CK" + countFormat.format(1);
		}else{
		    aa = "CK" + countFormat.format(num+1);
		}
		int addOut = distributorOrderMgtDao.addOut(detail,uuid,shipStore,shipUser,aa);
		if (addOut==1) {
			for (ProductDetail producDetail : detail.getProductDetails()) {
				int addOutDetail = distributorOrderMgtDao.addOutDetail(producDetail,mId,uuid);
			}
		}
		int result = distributorOrderMgtDao.approve(mId, dId, orderId,shipStore,createUser);
		return result;
	}

	@Override
	public int findNO(String mid, String did,int identification,String orderId,String no) {
		return distributorOrderMgtDao.findNO(mid,did,identification,orderId,no);
	}

}
